using System;
using Game;

namespace IndustrialAge {
    /// <summary>
    /// 用于工业的6x6合成台的Manager
    /// </summary>
    public static class NewCraftingRecipesManager {
        public static CraftingRecipe FindMatchingRecipe(SubsystemTerrain terrain, string[] ingredients, float heatLevel, float playerLevel) {
            CraftingRecipe craftingRecipe = null;
            Block[] blocks = BlocksManager.Blocks;
            for (int i = 0; i < blocks.Length; i++)
            {
                CraftingRecipe adHocCraftingRecipe = blocks[i].GetAdHocCraftingRecipe(terrain, ingredients, heatLevel, playerLevel);
                if (adHocCraftingRecipe != null && MatchRecipe(adHocCraftingRecipe.Ingredients, ingredients))
                {
                    craftingRecipe = adHocCraftingRecipe;
                    break;
                }
            }
            if (craftingRecipe == null)
            {
                foreach (CraftingRecipe recipe in CraftingRecipesManager.Recipes)
                {
                    if (MatchRecipe(recipe.Ingredients, ingredients))
                    {
                        craftingRecipe = recipe;
                        break;
                    }
                }
            }
            if (craftingRecipe != null)
            {
                if (heatLevel < craftingRecipe.RequiredHeatLevel)
                {
                    craftingRecipe = (!(heatLevel > 0f)) ? new CraftingRecipe
                    {
                        Message = LanguageControl.Get(CraftingRecipesManager.fName, 0)
                    } : new CraftingRecipe
                    {
                        Message = LanguageControl.Get(CraftingRecipesManager.fName, 1)
                    };
                }
                else if (playerLevel < craftingRecipe.RequiredPlayerLevel)
                {
                    craftingRecipe = (!(craftingRecipe.RequiredHeatLevel > 0f)) ? new CraftingRecipe
                    {
                        Message = String.Format(LanguageControl.Get(CraftingRecipesManager.fName, 2), craftingRecipe.RequiredPlayerLevel)
                    } : new CraftingRecipe
                    {
                        Message = String.Format(LanguageControl.Get(CraftingRecipesManager.fName, 3), craftingRecipe.RequiredPlayerLevel)
                    };
                }
            }
            return craftingRecipe;
#if false
                        Block[] blocks = BlocksManager.Blocks;
            for (int i = 0; i < blocks.Length; i++) {
                CraftingRecipe adHocCraftingRecipe = blocks[i].GetAdHocCraftingRecipe(terrain, ingredients, heatLevel, 1);
                if (adHocCraftingRecipe == null) continue;
                if (adHocCraftingRecipe != null
                    && adHocCraftingRecipe.RequiredHeatLevel == 0
                    && heatLevel >= adHocCraftingRecipe.RequiredHeatLevel
                    && CraftingRecipesManager.MatchRecipe(adHocCraftingRecipe.Ingredients, ingredients)) {
                    return adHocCraftingRecipe;
                }
            }
            int count = CraftingRecipesManager.Recipes.Count;
            for (int i = 0; i < count; i++) {
                CraftingRecipe adHocCraftingRecipe = CraftingRecipesManager.Recipes[i];
                if (adHocCraftingRecipe == null) continue;
                if (heatLevel >= adHocCraftingRecipe.RequiredHeatLevel
                    && adHocCraftingRecipe.RequiredHeatLevel == 0
                    && CraftingRecipesManager.MatchRecipe(adHocCraftingRecipe.Ingredients, ingredients)) {
                    return adHocCraftingRecipe;
                }
            }
            return null;
#endif
        }

        public static bool MatchRecipe(string[] requiredIngredients, string[] actualIngredients)
        {
            var array = new string[36];
            for (int i = 0; i < 2; i++)
            for (int j = 0; j <= 6; j++)
            for (int k = 0; k <= 6; k++)
            {
                bool flip = i != 0;
                if (TransformRecipe(array, requiredIngredients, k, j, flip))
                {
                    bool flag = true;
                    for (int l = 0; l < 36; l++)
                    {
                        if (!CraftingRecipesManager.CompareIngredients(array[l], actualIngredients[l]))
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                        return true;
                }
            }
            return false;
        }

        public static bool TransformRecipe(string[] transformedIngredients, string[] ingredients, int shiftX, int shiftY, bool flip)
        {
            Array.Clear(transformedIngredients, 0, 36);
            for (int j = 0; j < 6; j++)
            {
                for (int k = 0; k < 6; k++)
                {
                    int num = (flip ? (6 - k - 1) : k) + shiftX;
                    int num2 = j + shiftY;
                    string text = ingredients[k + j * 6];
                    if (num >= 0 && num2 >= 0 && num < 6 && num2 < 6)
                        transformedIngredients[num + num2 * 6] = text;
                    else if (!string.IsNullOrEmpty(text))
                        return false;
                }
            }
            return true;
        }
    }
}